package cn.edu.ren.controller;

import cn.edu.ren.entity.Student;
import cn.edu.ren.service.IStudentService;
import com.netflix.discovery.EurekaClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;

/**
 * @PackageName: cn.edu.ren
 * @ClassName: StudentController
 * @Description:
 * @author: RZS
 * @date: 2021/6/16  23:57
 */
@Api(tags = "student功能控制器")
@RestController
@RequestMapping("/student")
public class StudentController {

    @Autowired
    private IStudentService studentService;

    @Autowired
    private EurekaClient eurekaClient;

    @Autowired
    private DiscoveryClient discoveryClient;
    @GetMapping("/article/infos/disCli")
    public Object serviceUrl2() {
        return discoveryClient.getInstances("eureka-client");
    }

    /**
     * 获取自定义的元数据
     * @return
     */
    @GetMapping("/article/infos")
    public Object serviceUrl1() {
        return eurekaClient.getInstancesByVipAddress( "eureka-client", false);
    }

    /**
     * 注解@Value的作用：将application.yml文件中的指读取出来，然后赋值给变量
     * 这里是将application.yml文件中server.port对应的指读取出来并设置给变量port
     */
    @Value("${server.port}")
    private String port;

    @ApiOperation("查询所有student数据接口")
    @GetMapping("/find_all")
    public Collection<Student> findAll(){
        return studentService.selectAll();
    }

    @ApiOperation("根据id查询student")
    @ApiImplicitParam(name = "id", value = "id")
    @GetMapping("/find_by_id/{id}")
    public Student findnamById(@PathVariable("id")Integer id){
        return studentService.selectById(id);
    }

    @ApiOperation("添加student数据接口")
    @ApiImplicitParam(name = "student", value = "student对象")
    @PostMapping("/add")
    public void add(@RequestBody Student student){
        studentService.insert(student);
    }

    @ApiOperation("根据id删除student对象")
    @ApiImplicitParam(name = "id", value = "id")
    @DeleteMapping("/{id}")
    public void removeById(@PathVariable("id")Integer id){
        studentService.deleteById(id);
    }

    @ApiOperation("修改student数据接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "student", value = "student对象"),
            @ApiImplicitParam(name = "id", value = "id")
    })
    @PutMapping("/update/{id}")
    public void changeById(@RequestBody Student student, @PathVariable("id") Integer id){
        studentService.updateById(student, id);
    }

    @GetMapping("/index")
    public String index(){
        return "这是端口号为："+port+"的微服务";
    }
}
